﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Services;
using SER = CPPEI.ETL.Domain.Services;
using ENT = CPPEI.ETL.Domain.Entities;
using System.Data;

namespace CPPEI.ETL.WebApi.SYSWebService
{
    /// <summary>
    /// DataService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    public class DataService : System.Web.Services.WebService
    {
        [WebMethod]
        public List<DataSourceSummary> GetDataSources()
        {
            SER.PubSourceService service = new SER.PubSourceService();

            List<DataSourceSummary> dataSourceList = new List<DataSourceSummary>();

            ListResult<ENT.EPubDataSource> serResult = service.GetList();
            if(serResult.IsSuccess)
            {
                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    DataSourceSummary rawDataTable = new DataSourceSummary();
                    rawDataTable.Id = serResult.Data[index].Id;
                    rawDataTable.Name = serResult.Data[index].Name;
                    rawDataTable.Description = serResult.Data[index].Description;

                    dataSourceList.Add(rawDataTable);
                }
            }
            return dataSourceList;
        }

        [WebMethod]
        public List<DataParameter> GetParameters(Guid dataSourceId)
        {
            SER.PubSourceService service = new SER.PubSourceService();

            ServiceResult<CPPEI.ETL.Domain.Views.VPubDataSource> serResult = service.GetDetail(dataSourceId);

            List<DataParameter> parameters = new List<DataParameter>();

            for (int index = 0; index < serResult.Data.Parameters.Count; index++)
            {
                DataParameter parameter = new DataParameter();
                parameter.Id = serResult.Data.Parameters[index].Id;
                parameter.Name = serResult.Data.Parameters[index].Name;
                parameter.Type = serResult.Data.Parameters[index].Type;
                parameter.Description = serResult.Data.Parameters[index].Description;

                parameters.Add(parameter);
            }
            return parameters;
        }

        [WebMethod]
        public System.Data.DataTable GetData(Guid appId, string token, Guid dataSourceId, List<DataParameter> parameters)
        {
            SER.PubSourceService service = new SER.PubSourceService();

            List<string> parameterNames = new List<string>();
            List<string> parameterValues = new List<string>();
            for (int index = 0; index < parameters.Count; index++)
            {
                parameterNames.Add(parameters[index].Name);
                parameterValues.Add(parameters[index].Value);
            }
            ServiceResult<System.Data.DataTable> serResult = service.GetData(dataSourceId, parameterNames, parameterValues);

            if (serResult.IsSuccess)
            {
                return serResult.Data;
            }
            return null;
        }

        [WebMethod]
        public void Feedback(Guid appId, string token, Guid pubSourceId, Guid batchId, int dataCount, Guid[] passedData, Guid[] unpassedData)
        {
            SER.AppExtractService aeService = new SER.AppExtractService();
            ServiceResult<ENT.EAppExtractRecord> aeResult = aeService.CreateOrUpdate(appId, batchId, Domain.DEF.DataServiceTypeId.DataService, pubSourceId, dataCount, passedData, unpassedData);
            if (aeResult.IsSuccess)
            {
                //SER.RawDtTableService service = new SER.RawDtTableService();
                //service.CreateExtractHistory(appId, token, tableId, batchId, dataIdList);
            }
        }
    }
}
